﻿using System.IO;
using UnityEditor;
using UnityEngine;

using System.Text;
public class CreateAssetBundles
{
   // [MenuItem("CreateSRF/Windows")]
    static void BuildMulWinAssetBundles()
    {
        CheckDirectory();
        GameObject[] goList = Selection.gameObjects;
        int abSize = goList.Length;
        string[] path = new string[abSize];
        for (int i = 0; i < abSize; i++)
        {
            path[i] = AssetDatabase.GetAssetPath(goList[i]);
        }
        AssetBundleBuild[] abb = new AssetBundleBuild[1];
        abb[0].assetBundleName = goList[0].name + ".srf";
        abb[0].assetNames = new string[abSize];
        for (int i = 0; i < abSize; i++)
        {
            abb[0].assetNames[i] = path[i];
        }
        BuildPipeline.BuildAssetBundles("Assets/AssetBundles/Win", abb, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows);
        AssetDatabase.Refresh();
    }
    //[MenuItem("CreateSRF/Android")]
    static void BuildMulAndroidAssetBundles()
    {
        CheckDirectory();
        GameObject[] goList = Selection.gameObjects;
        int abSize = goList.Length;
        string[] path = new string[abSize];
        for (int i = 0; i < abSize; i++)
        {
            path[i] = AssetDatabase.GetAssetPath(goList[i]);
        }
        AssetBundleBuild[] abb = new AssetBundleBuild[1];
        abb[0].assetBundleName = goList[0].name + ".srf";
        abb[0].assetNames = new string[abSize];
        for (int i = 0; i < abSize; i++)
        {
            abb[0].assetNames[i] = path[i];
        }
        BuildPipeline.BuildAssetBundles("Assets/AssetBundles/Android", abb, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.Android);
        AssetDatabase.Refresh();
    }
    //[MenuItem("CreateSRF/IOS")]
    static void BuildMulIosAssetBundles()
    {
        CheckDirectory();
        GameObject[] goList = Selection.gameObjects;
        int abSize = goList.Length;
        string[] path = new string[abSize];
        for (int i = 0; i < abSize; i++)
        {
            path[i] = AssetDatabase.GetAssetPath(goList[i]);
        }
        AssetBundleBuild[] abb = new AssetBundleBuild[1];
        abb[0].assetBundleName = goList[0].name + ".srf";
        abb[0].assetNames = new string[abSize];
        for (int i = 0; i < abSize; i++)
        {
            abb[0].assetNames[i] = path[i];
        }
        BuildPipeline.BuildAssetBundles("Assets/AssetBundles/IOS", abb, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.iOS);
        AssetDatabase.Refresh();
    }
    //[MenuItem("CreateSRF/Holo")]
    static void BuildMulHoloAssetBundles()
    {
        CheckDirectory();
        GameObject[] goList = Selection.gameObjects;
        int abSize = goList.Length;
        string[] path = new string[abSize];
        for (int i = 0; i < abSize; i++)
        {
            path[i] = AssetDatabase.GetAssetPath(goList[i]);
        }
        AssetBundleBuild[] abb = new AssetBundleBuild[1];
        abb[0].assetBundleName = goList[0].name + ".srf";
        abb[0].assetNames = new string[abSize];
        for (int i = 0; i < abSize; i++)
        {
            abb[0].assetNames[i] = path[i];
        }
        BuildPipeline.BuildAssetBundles("Assets/AssetBundles/Hololens", abb, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.WSAPlayer);
        AssetDatabase.Refresh();
    }
    public static void ClearConsole()
    {
        //清空控制台Log信息--------------------------------------------------------------------------
        var logEntries = System.Type.GetType("UnityEditorInternal.LogEntries,UnityEditor.dll");
        var clearMethod = logEntries.GetMethod("Clear", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
        clearMethod.Invoke(null, null);
        //-------------------------------------------------------------------------------------------
    }
    static void CheckDirectory()
    {
        ClearConsole();
        string assetBundlesRootName = "AssetBundles";
        string assetBundlesRootPath = Application.dataPath + "/" + assetBundlesRootName;
        string androidRootPath = assetBundlesRootPath + "/Android";
        string winRootPath = assetBundlesRootPath + "/Win";
        string iosRootPath = assetBundlesRootPath + "/IOS";
        string hololensRootPath = assetBundlesRootPath + "/Hololens";
        string sdfRootPath = assetBundlesRootPath + "/SDF";
        if (!Directory.Exists(assetBundlesRootPath))
        {
            Debug.Log("项目中没有需要的文件夹，直接创建" + assetBundlesRootPath);
            AssetDatabase.CreateFolder("Assets", assetBundlesRootName);
        }
        if (!Directory.Exists(androidRootPath))
        {
            AssetDatabase.CreateFolder("Assets/" + assetBundlesRootName, "Android");
        }
        if (!Directory.Exists(winRootPath))
        {
            AssetDatabase.CreateFolder("Assets/" + assetBundlesRootName, "Win");
        }
        if (!Directory.Exists(iosRootPath))
        {
            AssetDatabase.CreateFolder("Assets/" + assetBundlesRootName, "IOS");
        }
        if (!Directory.Exists(hololensRootPath))
        {
            AssetDatabase.CreateFolder("Assets/" + assetBundlesRootName, "Hololens");
        }
        if (!Directory.Exists(sdfRootPath))
        {
            AssetDatabase.CreateFolder("Assets/" + assetBundlesRootName, "SDF");
        }
    }
    [MenuItem("GameObject/ExportJSON")]

    static void ExportJSON()

    {

        string filepath = Application.dataPath + @"/StreamingAssets/json.txt";

        FileInfo t = new FileInfo(filepath);

        if (!File.Exists(filepath))

        {

            File.Delete(filepath);

        }

        StreamWriter sw = t.CreateText();
    }
}
    #region
    //[MenuItem("CreateAB/Get AssetBundle names")]
    //static void GetNames()
    //{
    //    var names = AssetDatabase.GetAllAssetBundleNames();
    //    foreach (var name in names)
    //        Debug.Log("AssetBundle: " + name);
    //}

    //[MenuItem("CreateAB/CreateAll AssetBundles")]
    //static void CreateAssetBunldesMain()
    //{
    //    Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);

    //    foreach (Object obj in SelectedAsset)
    //    {
    //        string targetWinPath = Application.dataPath + "/StreamingAssets/windows/" + obj.name + ".assetbundle";

    //        if (BuildPipeline.BuildAssetBundle(obj, null, targetWinPath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.StandaloneWindows))
    //        {
    //            Debug.Log(obj.name + "资源Windows制作成功");
    //        }
    //        else
    //        {
    //            Debug.Log(obj.name + "资源Windows制作失败");
    //        }


    //        string targetIosPath = Application.dataPath + "/StreamingAssets/ios/" + obj.name + ".assetbundle";
    //        if (BuildPipeline.BuildAssetBundle(obj, null, targetIosPath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.iOS))
    //        {
    //            Debug.Log(obj.name + "资源Ios制作成功");
    //        }
    //        else
    //        {
    //            Debug.Log(obj.name + "资源Ios制作失败");
    //        }

    //        string targetAndroidPath = Application.dataPath + "/StreamingAssets/android/" + obj.name + ".assetbundle";
    //        if (BuildPipeline.BuildAssetBundle(obj, null, targetAndroidPath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.Android))
    //        {
    //            Debug.Log(obj.name + "资源Android制作成功");
    //        }
    //        else
    //        {
    //            Debug.Log(obj.name + "资源Android制作失败");
    //        }


    //        string targetOsxPath = Application.dataPath + "/StreamingAssets/osx/" + obj.name + ".assetbundle";
    //        if (BuildPipeline.BuildAssetBundle(obj, null, targetOsxPath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.StandaloneOSXUniversal))
    //        {
    //            Debug.Log(obj.name + "资源osx制作成功");
    //        }
    //        else
    //        {
    //            Debug.Log(obj.name + "资源osx制作失败");
    //        }


    //    }
    //    AssetDatabase.Refresh();

    //}
    #endregion

